<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Device\Repository;

use Plugin\Device\Model\RdDeviceBroadcast;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 广播设备 Repository类
 */
class RdDeviceBroadcastRepository extends IRepository
{
   public function __construct(
        protected readonly RdDeviceBroadcast $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 设备编码
        if (isset($params['code']) && filled($params['code'])) {
            $query->where('code', 'like', '%'.$params['code'].'%');
        }

        // 设备名称
        if (isset($params['name']) && filled($params['name'])) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        // 服务器id
        if (isset($params['server_id']) && filled($params['server_id'])) {
            $query->where('server_id', '=', $params['server_id']);
        }

        // 设备ip
        if (isset($params['ip']) && filled($params['ip'])) {
            $query->where('ip', 'like', '%'.$params['ip'].'%');
        }

        // 设备编号
        if (isset($params['device_id']) && filled($params['device_id'])) {
            $query->where('device_id', '=', $params['device_id']);
        }

        // 装置ID
        if (isset($params['rod_id']) && filled($params['rod_id'])) {
            $query->where('rod_id', '=', $params['rod_id']);
        }

        // 设备状态
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 音量
        if (isset($params['volume']) && filled($params['volume'])) {
            $query->where('volume', '=', $params['volume']);
        }

        // 备注
        if (isset($params['remarks']) && filled($params['remarks'])) {
            $query->where('remarks', 'like', '%'.$params['remarks'].'%');
        }

        // 
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 更新者
        if (isset($params['updated_by']) && filled($params['updated_by'])) {
            $query->where('updated_by', '=', $params['updated_by']);
        }

        // 站点id
        if (isset($params['stand_id']) && filled($params['stand_id'])) {
            $query->where('stand_id', '=', $params['stand_id']);
        }

        return $query;
    }

    public function page(array $params = [], ?int $page = null, ?int $pageSize = null): array
    {
        $result = $this->perQuery($this->getQuery(), $params)
            ->with(['stand:id,name', 'rod:id,name', 'server:id,name,link,username,password'])
            ->paginate(
                perPage: $pageSize,
                pageName: static::PER_PAGE_PARAM_NAME,
                page: $page,
            );
        return $this->handlePage($result);
    }

    public function findById(mixed $id): mixed
    {
        return $this->getQuery()->whereKey($id)->with(['stand:id,name', 'rod:id,name', 'server:id,link,username,password,auth'])->first();
    }
}